/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.fxn.component;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

import com.futuremind.recyclerviewfastscroll.viewprovider.ViewBehavior;
import com.futuremind.recyclerviewfastscroll.viewprovider.VisibilityAnimationManager;

/**
 * description : ViewBehavior的实现类
 *
 * @since 2021-04-15
 */
public class CustomHandleBehavior implements ViewBehavior {
    private boolean isGrabbed;

    private final VisibilityAnimationManager visibilityManager;
    private final HandleAnimationManager grabManager;

    /**
     * 初始化CustomHandleBehavior
     *
     * @param visibilityManager  visibilityManage
     * @param grabManager HandleAnimationManager
     */
    public CustomHandleBehavior(VisibilityAnimationManager visibilityManager, HandleAnimationManager grabManager) {
        this.visibilityManager = visibilityManager;
        this.grabManager = grabManager;
    }

    @Override
    public void onHandleGrabbed() {
        isGrabbed = true;
        visibilityManager.show();
        grabManager.onGrab();
    }

    @Override
    public void onHandleReleased() {
        isGrabbed = false;
        visibilityManager.hide();
        grabManager.onRelease();
    }

    @Override
    public void onScrollStarted() {
        visibilityManager.show();
    }

    @Override
    public void onScrollFinished() {
        if (!isGrabbed) {
            visibilityManager.hide();
        }
    }

    static class HandleAnimationManager {
        private AnimatorProperty grabAnimator;
        private AnimatorProperty releaseAnimator;

        protected HandleAnimationManager(Component handle,
                                            AnimatorProperty grabAnimator, AnimatorProperty releaseAnimator) {
            if (grabAnimator != null) {
                this.grabAnimator = grabAnimator;
                this.grabAnimator.setTarget(handle);
            }
            if (releaseAnimator != null) {
                this.releaseAnimator = releaseAnimator;
                this.releaseAnimator.setTarget(handle);
            }
        }

        /**
         * 滑块拖拽时的回调
         */
        public void onGrab() {
            if (releaseAnimator != null) {
                releaseAnimator.cancel();
            }
            if (grabAnimator != null) {
                grabAnimator.start();
            }
        }

        /**
         * 滑块松开时的回调
         */
        public void onRelease() {
            if (grabAnimator != null) {
                grabAnimator.cancel();
            }
            if (releaseAnimator != null) {
                releaseAnimator.start();
            }
        }

        /**
         * HandleAnimationManager 的构建方法
         */
        public static class Builder {
            private Component handle;
            private AnimatorProperty grabAnimator;
            private AnimatorProperty releaseAnimator;

            /**
             * Builder
             *
             * @param handle Component
             */
            Builder(Component handle) {
                this.handle = handle;
            }

            /**
             * 设置 grabAnimator
             *
             * @param grabAnimator grabAnimator
             * @return Builder
             */
            public Builder withGrabAnimator(AnimatorProperty grabAnimator) {
                this.grabAnimator = grabAnimator;
                return this;
            }

            /**
             * 设置 ReleaseAnimator
             *
             * @param releaseAnimator releaseAnimator
             * @return Builder
             */
            public Builder withReleaseAnimator(AnimatorProperty releaseAnimator) {
                this.releaseAnimator = releaseAnimator;
                return this;
            }

            /**
             * build HandleAnimationManager
             *
             * @return HandleAnimationManager
             */
            public HandleAnimationManager build() {
                return new HandleAnimationManager(handle, grabAnimator, releaseAnimator);
            }
        }
    }
}
